package com.exwave.socialgame.myutils;

import com.exwave.socialgame.model_wolfgame.wolfdata.Event;
import com.exwave.socialgame.model_wolfgame.wolfdata.Player;
import com.exwave.socialgame.model_wolfgame.wolfdata.Room;
import com.google.gson.Gson;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public final class RedisForWolfUtils {

    private final RedisTemplate<String, String> redisTemplate;

    @Resource
    private Gson gson;

    public RedisForWolfUtils(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    //保存Player信息
    public Boolean savePlayer(Player player) {
        String pkey = "P" + player.getUid();
        redisTemplate.opsForValue().set(pkey, gson.toJson(player));
        return true;
    }

    //读取Player信息
    public Player readPlayer(int key) {
        String pkey = "P" + key;
        return gson.fromJson(redisTemplate.opsForValue().get(pkey), Player.class);
    }

    //删除Player信息
    public Boolean delPlayer(int key) {
        String pkey = "P" + key;
        return redisTemplate.delete(pkey);
    }

    //保存房间信息
    public Boolean saveRoom(Room room) {
        String rkey = "R" + room.getKey_Room();
        redisTemplate.opsForValue().set(rkey, gson.toJson(room));
        return true;
    }

    //获取房间信息
    public Room readRoom(int key) {
        String rkey = "R" + key;
        return gson.fromJson(redisTemplate.opsForValue().get(rkey), Room.class);
    }

    //删除Room信息
    public Boolean delRoom(int key) {
        String pkey = "R" + key;
        return redisTemplate.delete(pkey);
    }

    //写入Event事件，房间ID+房间天数+操作类型，确定唯一Event
    public Boolean saveEvent(int roomid, int day, int state, int operation, Event event) {
        redisTemplate.opsForValue().set(roomid + +day + state + operation + "", gson.toJson(event));
        return true;
    }

    //获取Event事件
    public Event readEvent(int roomid, int day, int state, int operation) {
        return gson.fromJson(redisTemplate.opsForValue().get(roomid + day + state + operation + ""), Event.class);
    }

    //删除Event事件，不用手动删除，直接整合到白天黑夜切换
    public Boolean delEvent(int roomid, int day, int state, int operation) {
        return redisTemplate.delete(roomid + day + state + operation + "");
    }

    //判断读取是否为空
    //首先是判断房间操作是否为空
    public Boolean isNotNull(int roomId, Integer operation) {
        String msg = redisTemplate.opsForValue().get(roomId + operation + "");
        return msg != null;
    }

    //判断房间或者Player是否存在，房间 R ; Player P;
    public Boolean isNotNull(String kind, int key) {
        String msg = redisTemplate.opsForValue().get(kind + key);
        return msg != null;
    }

    //判断同类型Event操作是否存在
    public Event exitEvent(int roomId, int day, int state, int operation) {
        return gson.fromJson(redisTemplate.opsForValue().get(roomId + day + state + operation + ""), Event.class);
    }

    //当前房间开启投票
    public void onVote(int roomId) {
        redisTemplate.opsForValue().set("vote" + roomId, "true");
    }

    //当前房间关闭投票
    public void offVote(int roomId) {
        redisTemplate.opsForValue().set("vote" + roomId, "false");
    }

    //判断当前房间是否可以投票
    public boolean isVote(int roomId) {
        String msg = redisTemplate.opsForValue().get("vote" + roomId);
        if ("true".equals(msg))
            return true;
        return false;
    }
}
